//
//  BVBaseRouter.m
//  AFNetworking
//
//  Created by 崔晋 on 2019/8/14.
//

#import "BVBaseRouter.h"
#import "BVBaseViewController.h"

@interface BVRouterUtil ()

@property (strong, nonatomic) NSMutableDictionary *routerDic;

@property (strong, nonatomic) dispatch_semaphore_t semaphore;

@end

@implementation BVRouterUtil

+ (instancetype)shareInstance {
    static BVRouterUtil *util = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        util = [[BVRouterUtil alloc] init];
    });
    return util;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _semaphore = dispatch_semaphore_create(1);
    }
    return self;
}

- (void)addRouter:(BVBaseRouter *)router moduleName:(NSString *)moduleName {
    [self lock];
    if ([router isKindOfClass:[BVBaseRouter class]]) {
        [self.routerDic setObject:router forKey:moduleName];
    }
    [self unLock];
}

- (BVBaseRouter *)routerForModuleName:(NSString *)moduleName {
    [self lock];
    BVBaseRouter *router = self.routerDic[moduleName];
    [self unLock];
    return router;
}

- (void)lock {
    dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);
}

- (void)unLock {
    dispatch_semaphore_signal(self.semaphore);
}

- (NSMutableDictionary *)routerDic {
    if (!_routerDic) {
        _routerDic = [NSMutableDictionary dictionary];
    }
    return _routerDic;
}

@end

@implementation BVBaseRouter

+ (UIViewController *)viewControllerWithUrl:(NSString *)url
                                properties:(NSDictionary *_Nullable)properties {
    BVBaseRouter *moduleRouter = [[BVRouterUtil shareInstance] routerForModuleName:[self moduleNameWithUrl:url]];
    NSLog(@"%@", moduleRouter);
    UIViewController *vc = [moduleRouter viewControllerWithPageName:[self pageNameWithUrl:url] properties:properties];
    if (!vc) {
        vc = [[BVBaseViewController alloc] init];
    }
    return [self createNavWithVC:vc];
}

+ (UIViewController *)viewControllerWithNoNavUrl:(NSString *)url
                                      properties:(NSDictionary *_Nullable)properties {
    BVBaseRouter *moduleRouter = [[BVRouterUtil shareInstance] routerForModuleName:[self moduleNameWithUrl:url]];
    UIViewController *vc = [moduleRouter viewControllerWithPageName:[self pageNameWithUrl:url] properties:properties];
    if (!vc) {
        vc = [[BVBaseViewController alloc] init];
    }
    return vc;
};

+ (void)pushToViewControllerWithUrl:(NSString *)url properties:(NSDictionary *)properties {
    UIViewController *vc = [self viewControllerWithNoNavUrl:url properties:properties];
    [[self currentViewController] pushViewController:vc animated:YES];
}

+ (void)presentViewControllerWithUrl:(NSString *)url properties:(NSDictionary *)properties {
    UIViewController *vc = [self viewControllerWithNoNavUrl:url properties:properties];
    [[self currentViewController] presentViewController:vc animated:YES completion:nil];
}


+ (NSString *)moduleNameWithUrl:(NSString *)url {
    NSArray<NSString *> *contents = [url componentsSeparatedByString:@"/"];
    return contents.firstObject;
}

+ (NSString *)pageNameWithUrl:(NSString *)url {
    NSArray<NSString *> *contents = [url componentsSeparatedByString:@"/"];
    NSArray<NSString *> *pageNameWithProperties = [contents.lastObject componentsSeparatedByString:@"?"];
    return pageNameWithProperties.firstObject;
}

+ (BVBaseNavigationController *)createNavWithVC:(__kindof UIViewController *)vc {
    BVBaseNavigationController *navigationVC = [[BVBaseNavigationController alloc] initWithRootViewController:vc];
    return navigationVC;
}

+ (UINavigationController *)currentViewController {
    UITabBarController *tabBarC = (UITabBarController *)[[[UIApplication sharedApplication] delegate] window].rootViewController;
    if (![tabBarC isKindOfClass:[UITabBarController class]]) {
        return nil;
    }
    UINavigationController *navC = (UINavigationController *)tabBarC.selectedViewController;
    return navC;
}


#pragma mark - 需要子类实现
- (UIViewController *)viewControllerWithPageName:(NSString *)pageName properties:(NSDictionary *)properties {
    return nil;
}
@end
